<template>
  <div class="app-container">
    <div class="searchBox">
      <el-form :inline="true" :model="search" ref="searchForm">
        <el-form-item label="标题：" prop="scheduleTitle">
          <el-input v-model="search.scheduleTitle" name="scheduleTitle" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="类型：" prop="scheduleType">
          <el-select v-model="search.scheduleType" placeholder="请选择">
            <el-option
              v-for="item in scheduleoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="search.startTime"
            type="date"
            placeholder="选择日期时间"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="search.endTime"
            type="date"
            placeholder="选择日期时间"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item style="margin-top: -2px">
          <el-button type="primary" @click="searchQuery">查询</el-button>
          <el-button type="info" @click="search = {};searchQuery()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <qm-table
      ref="qmTable"
      :data="query"
      keyProp="scheduleId"
      :menuOptions="{ width: 150 }"
      :viewFunc="viewFunc"
      :editFunc="editFunc"
      :removeFunc="removeFunc"
      :addFunc="addFunc"
    >
      <qm-table-column field="scheduleTitle">标题</qm-table-column>
      <qm-table-column field="startTime">开始时间</qm-table-column>
      <qm-table-column field="endTime">结束时间</qm-table-column>
      <qm-table-column field="dataTitle">关联业务名称</qm-table-column>
      <qm-table-column field="adrress">位置</qm-table-column>
      <qm-table-column field="memo">描述</qm-table-column>
      <qm-table-column field="taskStatus" :renderFunc="istask">任务完成情况</qm-table-column>
    </qm-table>
    <el-dialog
      :title="viewModel.scheduleType=='task'?'任务详情':'日程详情'"
      :visible.sync="particularsview"
      width="30%"
      @closed="particularsclose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-tabs v-model="particularName" @tab-click="particularClick">
        <el-tab-pane :label="viewModel.scheduleType=='task'?'任务':'日程'" name="detail">
          <el-row style="margin-top:10px">标题：{{this.viewModel.scheduleTitle}}</el-row>
          <el-row style="margin-top:10px">位 置：{{this.viewModel.adrress}}</el-row>
          <el-row
            style="margin-top:10px"
          >起止时间：{{this.viewModel.startTime}} - {{this.viewModel.endTime}}</el-row>
          <el-row style="margin-top:10px">开始提醒：{{this.viewModel.startWarnType}}</el-row>
          <el-row style="margin-top:10px">结束提醒：{{this.viewModel.endWarnType}}</el-row>
          <el-row style="margin-top:10px">
            负责人：
            <template v-for="user in participant">
              <el-tag
                type="warning"
                effect="dark"
                v-if="user.userType =='principal'"
                :key="user.userName"
                style="margin:5px"
              >{{user.userName}}</el-tag>
            </template>
          </el-row>
          <el-row style="margin-top:10px">
            参与人：
            <template v-for="user in participant">
              <el-tag
                effect="dark"
                v-if="user.userType =='participant'"
                :key="user.userName"
                style="margin:5px"
              >{{user.userName}}</el-tag>
            </template>
          </el-row>
          <el-row style="margin-top:10px">关联类型：{{this.viewModel.dataType}}</el-row>
          <el-row style="margin-top:10px">关联业务名称：{{this.viewModel.dataTitle}}</el-row>
          <el-row style="margin-top:10px">详细描述：{{this.viewModel.memo}}</el-row>
          <el-row
            style="margin-top:10px"
            v-if="viewModel.scheduleType=='任务'"
          >任务完成情况：{{this.viewModel.taskStatus}}</el-row>
          <el-row>
            <el-col :span="24">
              <qm-file-upload
                :dataKey="this.viewModel.scheduleId"
                title="附件上传"
                :editable="false"
                fileSize="20"
                fileType=".jpg,.png,.doc,.docx,.xls,.xlsx,.pdf,.txt,.zip,.rar"
                @uploaded="uploadSuccess"
                viewType="office"
              ></qm-file-upload>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane v-if="viewModel.scheduleType=='task'" label="处理情况" name="dispose">
          <div v-for="item in results" :key="item.resultId" style="margin-top:10px">
            <el-row>
              <el-button type="info" icon="el-icon-user-solid" circle></el-button>
              {{item.userId}}
            </el-row>
            <el-row style="margin-top: 5px;">完成情况：{{item.taskStatus}}</el-row>
            <el-row style="margin-top: 5px;">登记时间{{item.finishDate}}</el-row>
            <el-row style="margin-top: 5px;">详细描述：{{item.memo}}</el-row>
          </div>
          <template>
            <el-col :offset="10">
              <el-button style="margin-top:10px" type="primary" @click="sendUp = true">
                上报
                <i class="el-icon-edit"></i>
              </el-button>
            </el-col>
          </template>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    <el-dialog
      title="上报信息"
      @closed="taskClose"
      :visible.sync="sendUp"
      width="30%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        :model="task"
        :rules="taskrules"
        ref="task"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="完成情况" prop="taskStatus">
          <el-select v-model="task.taskStatus" placeholder="请选择">
            <el-option
              v-for="item in taskStatusoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细说明" prop="memo">
          <el-input
            type="textarea"
            maxlength="500"
            :autosize="{ minRows: 4, maxRows: 8}"
            placeholder="填写详细的描述(0/500字)"
            v-model="task.memo"
          ></el-input>
        </el-form-item>
        <el-form-item label="登记时间" prop="finishDate">
          <el-date-picker v-model="task.finishDate" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="sendUp = false">取 消</el-button>
        <el-button type="primary" @click="taskSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      width="40%"
      :visible.sync="scheduleVisible"
      @closed="scheduleclose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <template slot="title">
        <div class="titleZise">
          {{form.scheduleId!=null?'编辑':'新增'}}
          <el-radio-group @change="scheduleclose" v-model="radio" :disabled="form.scheduleId!=null">
            <el-radio-button label="日程"></el-radio-button>
            <el-radio-button label="任务"></el-radio-button>
          </el-radio-group>
        </div>
      </template>
      <el-form
        :model="form"
        status-icon
        :rules="scheduleRules"
        ref="form"
        label-width="120px"
        size="medium"
      >
        <el-form-item :hidden="radio == '任务'" prop="scheduleType" label="日程类型">
          <el-select v-model="form.scheduleType" placeholder="请选择">
            <el-option
              v-for="item in scheduleoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="scheduleTitle" label="标题">
          <el-input v-model="form.scheduleTitle" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="adrress" label="地址">
          <el-input v-model="form.adrress" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="起止时间">
          <el-date-picker
            v-model="time"
            value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['09:00:00', '18:00:00']"
          ></el-date-picker>
          <el-form-item hidden prop="isRepeat">
            <el-checkbox @change="repeat" v-model="form.isRepeat">重复</el-checkbox>
          </el-form-item>
        </el-form-item>
        <el-form-item>
          <el-row v-if="form.isRepeat">
            <el-col :span="5">重复类型：</el-col>
            <el-col :span="6">
              <el-form-item prop="repeatType">
                <el-select @change="repeatType" v-model="form.repeatType" placeholder="请选择">
                  <el-option
                    v-for="item in repeatoptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="form.repeatType == 'weeks'" style="margin-top: 15px">
            <el-col :span="5">重复频率(周)：</el-col>
            <el-checkbox-group v-model="repeatWeek">
              <el-checkbox
                v-for="week in weekoptions"
                :label="week.value"
                :key="week.value"
              >{{ week.label }}</el-checkbox>
            </el-checkbox-group>
          </el-row>
          <el-row v-if="form.repeatType == 'customCycle'" style="margin-top: 15px">
            <el-col :span="5">重复频率(天)：</el-col>
            <el-col :span="6">
              <el-form-item prop="repeatTimes">
                <el-input-number size="mini" v-model="form.repeatTimes" :min="1" :max="10"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="form.isRepeat && form.repeatType != 'weeks'" style="margin-top: 15px">
            <el-col :span="5">结束条件：</el-col>
            <el-col :span="6">
              <el-form-item prop="repeatEndType">
                <el-select @change="repeatEndType" v-model="form.repeatEndType" placeholder="请选择">
                  <el-option
                    v-for="item in repeatEndoptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="form.repeatEndType == 'finish'" :span="6" :offset="2">
              <el-form-item prop="repeatEndDate">
                <el-date-picker v-model="form.repeatEndDate" type="date" placeholder="选择日期"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="form.repeatType == 'weeks'" style="margin-top: 15px">
            <el-col :span="4">重复开始日期：</el-col>
            <el-col :span="8">
              <el-form-item prop="repeatStarDate">
                <el-date-picker v-model="form.repeatStarDate" type="date" placeholder="选择日期"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="4">重复结束日期：</el-col>
            <el-col :span="8">
              <el-form-item prop="repeatEndDate">
                <el-date-picker v-model="form.repeatEndDate" type="date" placeholder="选择日期"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="startWarnType" label="开始提醒">
          <el-select v-model="form.startWarnType" placeholder="请选择">
            <el-option
              v-for="item in WarnTypeoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="endWarnType" label="截止提醒">
          <el-select v-model="form.endWarnType" placeholder="请选择">
            <el-option
              v-for="item in WarnTypeoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-for="(domain, index) in functionary" :label="'参与人员'" :key="domain.key">
          <el-row>
            <el-col :span="6">
              <el-select v-model="domain.userType" placeholder="请选择人员类型">
                <el-option
                  v-for="item in userTypeoptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-col>
            <el-col :span="15" :offset="1">
              <el-select v-model="domain.userName" multiple filterable placeholder="请输入关键词">
                <el-option
                  v-for="item in users"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-col>
            <el-col :span="1" :offset="1">
              <i v-if="index >= 1" class="el-icon-error" @click.prevent="removeDomain(domain)"></i>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" icon="el-icon-plus" @click="addDomain">添加人员</el-button>
        </el-form-item>
        <el-form-item label="关联业务">
          <el-row>
            <el-col :span="8">
              <el-form-item prop="dataType">
                <el-select @change="selectdataTy" v-model="form.dataType" placeholder="请选择">
                  <el-option
                    v-for="item in dataTypeoptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="15" :offset="1">
              <el-form-item prop="dataKey">
                <el-select
                  v-model="form.dataKey"
                  filterable
                  remote
                  reserve-keyword
                  placeholder="请输入关键词"
                  :remote-method="remoteContract"
                  :loading="contractLoading"
                >
                  <el-option
                    v-for="item in contractOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="memo" label="详细描述">
          <el-input
            type="textarea"
            maxlength="500"
            :autosize="{ minRows: 4, maxRows: 8 }"
            placeholder="填写该任务更详细的描述(0/500字)"
            v-model="form.memo"
          ></el-input>
        </el-form-item>
        <el-form-item :hidden="radio == '日程'" prop="taskStatus" label="任务完成情况">
          <el-select v-model="form.taskStatus" placeholder="请选择">
            <el-option
              v-for="item in taskStatusoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-row>
            <el-col :span="24">
              <qm-file-upload
                :dataKey="scheduleId"
                :editable="true"
                title="附件上传"
                fileSize="20"
                fileType=".jpg,.png,.doc,.docx,.xls,.xlsx,.pdf,.txt,.zip,.rar"
                @uploaded="uploadSuccess"
              ></qm-file-upload>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="scheduleVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitSchedule('form')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Controller from "@/libs/framework/controller";
import request from "@/utils/request";
import QmFileUpload from "@/libs/components/qm-file-upload";
export default class Instance extends Controller {
  onBeforeInit() {
    this.urls = {
      query: [{ url: "/admin/scheduleTask/query", method: "post" }],
    };
  }

  mixin() {
    return {
      components: {
        QmFileUpload,
      },
      created() {
        this.remoteMethod();
        this.getModel();
      },
      data() {
        return {
          //表单实体
          model: null,
          //用于附件的dataKey
          scheduleId: null,
          time: [],
          //下拉框数据
          users: [],
          userTypeoptions: [
            {
              value: "principal",
              label: "负责人",
            },
            {
              value: "participant",
              label: "参与人",
            },
          ],
          //负责人员
          functionary: [
            {
              userType: null,
              userName: [],
            },
          ],
          WarnTypeoptions: [
            {
              value: "noWarn",
              label: "不提醒",
            },
            {
              value: "punctuality",
              label: "准时",
            },
            {
              value: "fiveMinutes",
              label: "提前5分钟",
            },
            {
              value: "tenMinutes",
              label: "提前10分钟",
            },
            {
              value: "thirtyMinutes",
              label: "提前30分钟",
            },
            {
              value: "oneHours",
              label: "提前1小时",
            },
            {
              value: "twoHours",
              label: "提前2小时",
            },
            {
              value: "sixHours",
              label: "提前6小时",
            },
            {
              value: "oneDay",
              label: "提前1天",
            },
            {
              value: "twoDay",
              label: "提前2天",
            },
          ],
          repeatEndoptions: [
            {
              value: "never",
              label: "永不结束",
            },
            {
              value: "finish",
              label: "结束日期",
            },
          ],
          weekoptions: [
            { value: 1, label: "每周一" },
            { value: 2, label: "每周二" },
            { value: 3, label: "每周三" },
            { value: 4, label: "每周四" },
            { value: 5, label: "每周五" },
            { value: 6, label: "每周六" },
            { value: 0, label: "每周日" },
          ],
          repeatoptions: [
            {
              value: "everyday",
              label: "每天",
            },
            {
              value: "everyTuesday",
              label: "每周二",
            },
            {
              value: "every30",
              label: "每月30日",
            },
            {
              value: "weeks",
              label: "周",
            },
            {
              value: "customCycle",
              label: "自定义周期",
            },
          ],
          scheduleRules: {
            scheduleTitle: [
              { required: true, message: "请填写标题", trigger: "blur" },
              {
                min: 1,
                max: 50,
                message: "长度在1-50 个字符以内",
                trigger: "blur",
              },
            ],
            adrress: [
              { required: true, message: "请填写地址", trigger: "blur" },
              {
                min: 1,
                max: 50,
                message: "长度在1-50 个字符以内",
                trigger: "blur",
              },
            ],
            //日程类别
            scheduleType: [
              { required: true, message: "请选择日程类型", trigger: "change" },
            ],
            startWarnType: [
              {
                required: true,
                message: "请选择开始提醒类型",
                trigger: "change",
              },
            ],
            endWarnType: [
              {
                required: true,
                message: "请选择结束提醒类型",
                trigger: "change",
              },
            ],
            memo: [
              { required: true, message: "请填写详细描述", trigger: "blur" },
              {
                min: 1,
                max: 500,
                message: "长度在1-500 个字符以内",
                trigger: "blur",
              },
            ],
            //任务完成情况
            taskStatus: [
              {
                required: true,
                message: "请选择任务完成情况",
                trigger: "change",
              },
            ],
          },
          radio: "日程",
          //日程
          scheduleVisible: false,
          task: {
            scheduleId: null,
            taskStatus: null,
            memo: null,
            finishDate: null,
          },
          taskrules: {
            memo: [
              { required: true, message: "请填写详细描述", trigger: "blur" },
              {
                min: 1,
                max: 500,
                message: "长度在1-500 个字符以内",
                trigger: "blur",
              },
            ],
            taskStatus: [
              { required: true, message: "请选择完成情况", trigger: "blur" },
            ],
            finishDate: [
              {
                type: "date",
                required: true,
                message: "请选择日期",
                trigger: "blur",
              },
            ],
          },
          //处理情况
          sendUp: false,
          results: null,
          form: {
            parentId: "",
            scheduleType: "",
            scheduleTitle: "",
            adrress: "",
            startTime: "",
            endTime: "",
            isRepeat: null,
            repeatType: "",
            repeatTimes: "",
            repeatWeeks: "",
            repeatEndType: "",
            repeatStarDate: "",
            repeatEndDate: "",
            startWarnType: "",
            endWarnType: "",
            dataType: "",
            dataKey: "",
            dataTitle: "",
            memo: "",
            taskFinishType: "",
            taskStatus: "",
            //附件id
            attachmentIds: null,
          },
          taskStatusoptions: [
            {
              value: "finished",
              label: "已完成",
            },
            {
              value: "unfinished",
              label: "未完成",
            },
            {
              value: "postpone",
              label: "推迟",
            },
          ],
          scheduleoptions: [
            {
              value: "visitingClients",
              label: "拜访客户",
            },
            {
              value: "conference",
              label: "会议",
            },
            {
              value: "rest",
              label: "其他",
            },
            {
              value: "task",
              label: "任务",
            },
          ],
          scheduleoptions2: [
            {
              value: "visitingClients",
              label: "拜访客户",
            },
            {
              value: "conference",
              label: "会议",
            },
            {
              value: "rest",
              label: "其他",
            },
          ],
          viewModel: {
            parentId: "",
            scheduleType: "",
            scheduleTitle: "",
            adrress: "",
            startTime: "",
            endTime: "",
            isRepeat: null,
            repeatType: "",
            repeatTimes: "",
            repeatEndType: "",
            repeatEndDate: "",
            startWarnType: "",
            endWarnType: "",
            dataType: "",
            dataKey: "",
            dataTitle: "",
            memo: "",
            taskFinishType: "",
            taskStatus: "",
            //附件id
            attachmentIds: null,
          },
          //参与人员
          participant: [{ userName: "" }],
          particularsview: false,
          particularName: "detail",
          //关联项目下拉框数据
          contractOptions: [],
          contractLoading: false,
          dataTypeoptions: [
            {
              value: "customer",
              label: "客户",
            },
            {
              value: "chance",
              label: "商机",
            },
            {
              value: "contract",
              label: "合同",
            },
            {
              value: "project",
              label: "项目",
            },
          ],
          //重复周
          repeatWeek: [],
        };
      },
      methods: {
        getModel() {
          request({
            url: "/admin/scheduleTask/getEntity",
            method: "post",
          })
            .then((r) => {
              if (r && !r.initMessage) {
                this.model = r.models.model;
              } else {
                this.$message.error(r.initMessage || "数据请求失败");
              }
            })
            .catch((e) => {
              this.$message.error(e);
            });
        },
        addFunc() {
          this.scheduleVisible = true;
        },
        editFunc(row) {
          this.scheduleVisible = true;
          this.$nextTick(function () {
            //重新取值赋值
            request({
              url: "/admin/scheduleTask/getScheduleTask",
              method: "get",
              params: {
                scheduleId: row.scheduleId,
              },
            })
              .then((r) => {
                if (r && !r.initMessage) {
                  this.form = r.parameters.scheduleTask;
                  if (this.form.scheduleType == "task") {
                    this.radio = "任务";
                  } else {
                    this.radio = "日程";
                  }
                  this.form.isRepeat = this.form.isRepeat ? true : false;
                  //时间
                  this.time.push(new Date(this.form.startTime));
                  this.time.push(new Date(this.form.endTime));
                  //参与人员
                  this.functionary = r.parameters.functionary;
                  //附件
                  this.scheduleId = this.form.scheduleId;
                  let dataKey = this.form.dataKey;
                  //关联人员
                  if (this.form.dataType != "") {
                    this.selectdataTy(this.form.dataType);
                    this.form.dataKey = dataKey;
                  }
                  //回显周
                  if (this.form.repeatWeeks != null) {
                    var dataStrArr = this.form.repeatWeeks.split(","); //分割成字符串数组
                    this.repeatWeek = dataStrArr.map(Number);
                  }
                } else {
                  this.$message.error(r.initMessage || "数据请求失败");
                }
              })
              .catch((e) => {
                this.$message.error(e);
              });
          });
        },
        submitSchedule(formName) {
          if (this.radio == "日程") {
            this.form.taskStatus = "暂无";
          } else {
            this.form.scheduleType = "task";
          }
          this.$refs[formName].validate((valid) => {
            if (valid) {
              if (this.time.length > 1) {
                let postModel = _.extend({}, this.form);
                //将时间进行赋值
                postModel.startTime = formatter.formatDateTime(this.time[0]);
                postModel.endTime = formatter.formatDateTime(this.time[1]);
                postModel.repeatEndDate = formatter.formatDateTime(
                  postModel.repeatEndDate
                );
                postModel.repeatStarDate = formatter.formatDateTime(
                  postModel.repeatStarDate
                );
                //关联title
                if (postModel.dataKey != null) {
                  this.contractOptions.forEach((item) => {
                    if (item.value == postModel.dataKey) {
                      postModel.dataTitle = item.label;
                      return false;
                    }
                  });
                }
                postModel.isRepeat = postModel.isRepeat ? "1" : "0";
                //重复为周重组
                if (postModel.repeatType == "weeks") {
                  postModel.repeatWeeks = this.repeatWeek.toString();
                } else {
                  postModel.repeatWeeks = null;
                }
                //创建人、参与人
                let newFunctionary = [];
                this.functionary.forEach((item) => {
                  if (item.userType != null) {
                    item.userName.forEach((user) => {
                      newFunctionary.push({
                        userId: user,
                        userType: item.userType,
                      });
                    });
                  }
                });
                if (postModel.scheduleId != null) {
                  request({
                    url: "/admin/scheduleTask/editScheduleTask",
                    method: "post",
                    data: {
                      scheduleTask: postModel,
                      functionary: newFunctionary,
                    },
                  })
                    .then((r) => {
                      if (r && !r.initMessage) {
                        this.scheduleVisible = false;
                        this.$message({
                          type: "success",
                          message: "修改成功!",
                        });
                        this.$refs["qmTable"].refresh();
                      } else {
                        this.$message.error(r.initMessage || "数据请求失败");
                      }
                    })
                    .catch((e) => {
                      this.$message.error(e);
                    });
                } else {
                  request({
                    url: "/admin/scheduleTask/addScheduleTask",
                    method: "post",
                    data: {
                      scheduleTask: postModel,
                      functionary: newFunctionary,
                      attachmentIds: postModel.attachmentIds,
                      radio: this.radio,
                    },
                  })
                    .then((r) => {
                      if (r && !r.initMessage) {
                        this.scheduleVisible = false;
                        this.$refs["qmTable"].refresh();
                        this.$message.success("添加成功");
                      } else {
                        this.$message.error(r.initMessage || "数据请求失败");
                      }
                    })
                    .catch((e) => {
                      this.$message.error(e);
                    });
                }
              } else {
                this.$message({
                  type: "warning",
                  message: "请选择起止时间!",
                });
                return false;
              }
            } else {
              return false;
            }
          });
        },
        //远程搜索
        remoteContract(query) {
          if (query !== "") {
            this.contractLoading = true;
            setTimeout(() => {
              this.contractLoading = false;
              this.contractOptions = this.contractOptions.filter((item) => {
                return (
                  item.label.toLowerCase().indexOf(query.toLowerCase()) > -1
                );
              });
            }, 200);
          } else {
            this.contractOptions = [];
          }
        },
        selectdataTy(val) {
          this.form.dataKey = null;
          request({
            url: "/admin/scheduleTask/getDataOptions",
            method: "get",
            params: {
              dataType: val,
            },
          })
            .then((r) => {
              if (r && !r.initMessage) {
                this.contractOptions = r.parameters.dataOptions;
              } else {
                this.$message.error(r.initMessage || "数据请求失败");
              }
            })
            .catch((e) => {
              this.$message.error(e);
            });
        },
        addDomain() {
          this.functionary.push({
            userType: null,
            userName: [],
            key: Date.now(),
          });
        },
        removeDomain(item) {
          var index = this.functionary.indexOf(item);
          if (index !== -1) {
            this.functionary.splice(index, 1);
          }
        },
        remoteMethod() {
          request({
            url: "/admin/scheduleTask/userSelect2",
            method: "get",
          })
            .then((r) => {
              if (r && !r.initMessage) {
                this.users = r.data.map((m) => {
                  return {
                    value: m.userId,
                    label: m.userName,
                  };
                });
              } else {
                this.$message.error(r.initMessage || "数据请求失败");
              }
            })
            .catch((e) => {
              this.$message.error(e);
            });
        },
        repeatEndType(value) {
          if (value == "never") {
            this.form.repeatEndDate = "";
          }
        },
        repeatType(value) {
          if (value != "customCycle") {
            this.form.repeatTimes = "";
          }
        },
        //重复
        repeat(value) {
          if (value) {
            this.form.repeatType = this.repeatoptions[0].value;
            this.form.repeatEndType = this.repeatEndoptions[0].value;
          } else {
            this.form.repeatType = "";
            this.form.repeatTimes = "";
            this.form.repeatEndType = "";
            this.form.repeatEndDate = "";
          }
        },
        scheduleclose() {
          this.functionary = [
            {
              userType: null,
              userName: [],
            },
          ];
          this.scheduleId = null;
          this.time = [];
          //置空数据，清除校验
          this.$refs["form"].resetFields();
          let postModel = _.extend({}, this.model);
          this.form = postModel;
        },
        taskSubmit() {
          this.$refs["task"].validate((valid) => {
            if (valid) {
              this.task.scheduleId = this.viewModel.scheduleId;
              this.task.finishDate = formatter.formatDateTime(
                this.task.finishDate
              );
              request({
                url: "/admin/scheduleResult/addScheduleResult",
                method: "post",
                data: {
                  task: this.task,
                },
              })
                .then((r) => {
                  if (r && !r.initMessage) {
                    this.sendUp = false;
                    this.$message({
                      type: "success",
                      message: "上报成功!",
                    });
                    this.getScheduleResult();
                  } else {
                    this.$message.error(r.initMessage || "数据请求失败");
                  }
                })
                .catch((e) => {
                  this.$message.error(e);
                });
            } else {
              return false;
            }
          });
        },
        taskClose() {
          this.$refs["task"].resetFields();
        },
        uploadSuccess(attachment) {
          this.form.attachmentIds =
            (this.form.attachmentIds || "") + `,${attachment.attachmentId}`;
        },
        removeFunc(rows) {
          if (rows.length == 0) {
            this.$message({
              type: "warning",
              message: "请选择至少一条数据!",
            });
            return;
          }
          this.$confirm("确定删除此条日程信息吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              request({
                url: "/admin/scheduleTask/deleteScheduleTask",
                method: "post",
                data: {
                  scheduleIds: rows,
                },
              })
                .then((r) => {
                  if (r && !r.initMessage) {
                    this.$message({
                      type: "success",
                      message: "删除成功!",
                    });
                    this.$refs["qmTable"].refresh();
                  } else {
                    this.$message.error(r.initMessage || "数据请求失败");
                  }
                })
                .catch((e) => {
                  this.$message.error(e);
                });
            })
            .catch(() => {});
        },
        viewFunc(row) {
          request({
            url: "/admin/scheduleTask/getSome",
            method: "get",
            params: {
              scheduleId: row.scheduleId,
            },
          })
            .then((r) => {
              if (r && !r.initMessage) {
                this.viewModel = r.parameters.scheduleTask;
                this.participant = r.parameters.users;
              } else {
                this.$message.error(r.initMessage || "数据请求失败");
              }
            })
            .catch((e) => {
              this.$message.error(e);
            });
          this.particularsview = true;
        },
        istask(val) {
          if (val == null) {
            return "暂无";
          } else {
            let value;
            this.taskStatusoptions.forEach((item) => {
              if (val == item.value) {
                value = item.label;
              }
            });
            return value;
          }
        },
        getScheduleResult() {
          request({
            url: "/admin/scheduleResult/getScheduleResult",
            method: "get",
            params: {
              scheduleId: this.viewModel.scheduleId,
            },
          })
            .then((r) => {
              if (r && !r.initMessage) {
                this.results = r.parameters.results;
              } else {
                this.$message.error(r.initMessage || "数据请求失败");
              }
            })
            .catch((e) => {
              this.$message.error(e);
            });
        },
        particularClick(tab) {
          if (tab.name == "dispose") {
            this.getScheduleResult();
          }
        },
        particularsclose() {
          this.particularName = "detail";
        },
        //任务详情
        particulars(id) {
          request({
            url: "/admin/scheduleTask/getSome",
            method: "get",
            params: {
              scheduleId: id,
            },
          })
            .then((r) => {
              if (r && !r.initMessage) {
                this.viewModel = r.parameters.scheduleTask;
                this.participant = r.parameters.users;
              } else {
                this.$message.error(r.initMessage || "数据请求失败");
              }
            })
            .catch((e) => {
              this.$message.error(e);
            });
          this.particularsview = true;
        },
      },
    };
  }
}
</script>

<style scoped lang="scss">
</style>

